package com.example.demo.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.security.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户
 */
@Data
@NoArgsConstructor
public class User implements UserDetails {
   private Integer id;
   private String username;
   private String password;
   private boolean enabled = true;  //设置当前用户是否正常
   private List<Role> roles;
   @Override
   @JsonIgnore
   public boolean isAccountNonExpired() { // 帐户是否过期
       return true;
   }

   @Override
   @JsonIgnore
   public boolean isAccountNonLocked() { // 帐户是否被冻结
       return true;
   }

    // 帐户密码是否过期，一般有的密码要求性高的系统会使用到，比较每隔一段时间就要求用户重置密码
   @Override
   @JsonIgnore
   public boolean isCredentialsNonExpired() { 
       return true;
   }

   @Override
   public boolean isEnabled() {  // 帐号是否可用
       return enabled;
   }

   public void setEnabled(boolean enabled) {
       this.enabled = enabled;
   }

   @Override
   @JsonIgnore
   public List<GrantedAuthority> getAuthorities() {
       List<GrantedAuthority> authorities = new ArrayList<>();
       for (Role role : roles) {
           authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName()));
       }
       return authorities;
   }


    public User(String username, String password, List<Role> roles) {
        this.username = username;
        this.password = password;
        this.roles = roles;
    }
}